<?php require_once('common.php');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kasir extends Common {
	function __construct() {
		parent::__construct("kasir");
		$this->load->model(array('pesanan'));
	}

	public function index(){	
		$data['pesanan'] = $this->pesanan->get(array('status != ' => STATUS_SUDAH_DIBAYAR), 'kasir')->result_array();
		$data['status'] = unserialize(STATUS_PESANAN);
		$data['pesan'] = $this->session->flashdata('pesan');
		$data['title'] = 'Pesanan yang sedang diproses';
		$data['is_history'] = 'no';
		$this->load->view('kasir/landing_page', $data);
	}
	
	public function histori_pesanan(){
		$data['pesanan'] = $this->pesanan->get(array('status' => STATUS_SUDAH_DIBAYAR))->result_array();
		$data['status'] = unserialize(STATUS_PESANAN);
		$data['pesan'] = NULL;
		$data['title'] = 'Histori Pesanan';
		$data['is_history'] = 'hist';
		$this->load->view('kasir/landing_page', $data);
	}
	
	public function detail_pesanan($id_pesanan, $hist){
		$this->load->model('user');
		$pesanan = $this->pesanan->get(array('id_pesanan ' => $id_pesanan))->row_array();
		$pesanan['detail'] = $this->pesanan->get_detail_pesanan($pesanan['id_pesanan']);
		$petugas = $this->user->get(array('id_petugas' => $pesanan['id_petugas']))->row_array();
		$pesanan['pelayan'] = $petugas['nama'];
		$data['pesanan'] = $pesanan;
		$data['status'] = unserialize(STATUS_PESANAN);
		$data['pesan'] = $this->session->flashdata('pesan');
		$data['is_history'] = ($hist == 'hist');
		$this->load->view('kasir/detail_pesanan', $data);
	}
	
	public function proses_bayar(){
		$id_pesanan = $this->input->post('id_pesanan');
		$waktu_pesan = $this->input->post('waktu_pesan');
		$no_meja = $this->input->post('no_meja');
		$data = array('jumlah_pesanan' => $this->input->post('jumlah_pesanan'),
					  'pajak' => $this->input->post('pajak'),
					  'diskon' => $this->input->post('diskon'),
					  'jumlah_bayar' => $this->input->post('jumlah_bayar'),
					  'status' => STATUS_SUDAH_DIBAYAR);
		if($this->pesanan->edit($id_pesanan, $data)){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Proses pembayaran pesanan berhasil disimpan. Klik menu <b>History Peminjaman</b> untuk melihat pesanan yang sudah dibayar"));
			$this->print_nota($data, $waktu_pesan, $no_meja, $id_pesanan);
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Proses pembayaran pesanan gagal disimpan."));
			redirect(base_url()."index.php/kasir");
		}
	}
	
	function print_nota($data, $waktu_pesan, $no_meja, $id_pesanan){
		$pesanan = $this->pesanan->get_detail_pesanan($id_pesanan);
		echo '<table cellpadding="10">
		<tr>
			<td>Waktu Pesan</td>
			<td>'. date('d M Y H:i', strtotime($waktu_pesan)).'</td>
		</tr>
		<tr>
			<td>No. Meja</td>
			<td>'.$no_meja.'</td>
		</tr>
		<tr>
			<td>Detail Pesanan</td>
			<td>';
				foreach($pesanan as $detail){ 
					$kali = $detail['harga'] * $detail['jumlah'];
					echo '<b>'.$detail['nama'].'('.$detail['jumlah'].')</b> | ';
					echo $detail['harga'] .' x '. $detail['jumlah'] . ' = ' . $kali .'<br/>';
				}
		echo '</td>
		</tr>
		<tr>
			<td>Jumlah Pesanan</td>
			<td>Rp. '.$data['jumlah_pesanan'].'</td>
		</tr>
		<tr>
			<td>Pajak</td>
			<td>Rp. '. $data['pajak'] .',-</td>
		</tr>
		<tr>
			<td>Diskon</td>
			<td>Rp. '.$data['diskon'] .',-</td>
		</tr>
		<tr>
			<td>Jumlah Bayar</td>
			<td>Rp. '.$data['jumlah_bayar'].',-</td>
		</tr>
		</table>';
		echo '<script>
				window.print();
				setTimeout(function () {
					window.location = "' . base_url() . 'index.php/kasir";
				}, 1000)';
		echo '</script>';
	}
	
	public function laporan_keuangan(){
		$data['pendapatan'] = $this->pesanan->get_income();
		if(isset($_POST['filter'])){
			$start_date = $this->input->post('start_date');
			$end_date = $this->input->post('end_date');
			$data['pendapatan'] = $this->pesanan->get_income($start_date, $end_date);
			$data['start_date'] = $start_date;
			$data['end_date'] = $end_date;
		}
		$this->load->view('kasir/laporan_keuangan', $data);
	}
	
}